import xml.EntailementCorpus;
import xml.Pair;
import xml.XMLParser;
import xmlpreprocessed.EntailmentCorpus;

import java.io.IOException;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: oye
 * Date: 04.11.11
 * Time: 11:47
 * To change this template use File | Settings | File Templates.
 */
public class RTEMain {

    public static void main(String[] args) {
        try {
            runWordMatching();
            runLemmaMatching();
            runBleuMatching();
            runIdfMatching();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public static void runWordMatching() throws IOException {
        System.out.println("Word matching in progress...");

        XMLParser parser = new XMLParser();

        EntailementCorpus entailementCorpus = parser.getEntailementCorpus("rte2_dev_data/RTE2_dev.xml");

        RTEPhase1 rtePhase1 = new RTEPhase1();
        
        double maxCorrect = 0;
        double maxCorrectThres = 0.0;
        for(int thres=0; thres<=100; thres++){
            List<Pair> pairs = entailementCorpus.getProperties();
            String predFileName = new String();
            try {
                predFileName = rtePhase1.wordMatching(pairs, (double)thres/100);
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }

            double tempCorrect = eval_rte.evaluateWordMatching(pairs, predFileName);
            if(tempCorrect>maxCorrect){
                maxCorrect = tempCorrect;
                maxCorrectThres = (double)thres/100;
            }
        }

        System.out.println("Word matching best correct : "+maxCorrect*100+"% @ Threshold: "+maxCorrectThres);
        try {
            rtePhase1.wordMatching(entailementCorpus.getProperties(), maxCorrectThres);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
    
    public static void runLemmaMatching() throws IOException {
        System.out.println("Lemma and LemmaPOS matching in progress...");

        xmlpreprocessed.XMLParser parser = new xmlpreprocessed.XMLParser();

        EntailmentCorpus entailmentCorpus = parser.getEntailementCorpus("rte2_dev_data/RTE2_dev.preprocessed.xml");

        RTEPhase1 rtePhase1 = new RTEPhase1();

        double lemmaMaxCorrect = 0;
        double lemmaMaxCorrectThres = 0.0;
        double lemmaPOSMaxCorrect = 0;
        double lemmaPOSMaxCorrectThres = 0.0;
        for(int thres=0; thres<=100; thres++){
            List<xmlpreprocessed.Pair> pairs = entailmentCorpus.getProperties();
            String lemmaPredFileName = new String();
            String lemmaPOSPredFileName = new String();
            try {
                lemmaPredFileName = rtePhase1.lemmaMatching(pairs, (double)thres/100);
                lemmaPOSPredFileName = rtePhase1.lemmaPOSMatching(pairs, (double)thres/100);
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }

            double tempLemmaCorrect = eval_rte.evaluateLemmaMatching(pairs, lemmaPredFileName);
            if(tempLemmaCorrect>lemmaMaxCorrect){
                lemmaMaxCorrect = tempLemmaCorrect;
                lemmaMaxCorrectThres = (double)thres/100;
            }

            double tempLemmaPOSCorrect = eval_rte.evaluateLemmaMatching(pairs, lemmaPOSPredFileName);
            if(tempLemmaPOSCorrect>lemmaPOSMaxCorrect){
                lemmaPOSMaxCorrect = tempLemmaPOSCorrect;
                lemmaPOSMaxCorrectThres = (double)thres/100;
            }
        }

        System.out.println("Lemma matching best correct : "+lemmaMaxCorrect+"% @ Threshold: "+lemmaMaxCorrectThres);
        System.out.println("LemmaPOS matching best correct : "+lemmaPOSMaxCorrect+"% @ Threshold: "+lemmaPOSMaxCorrectThres);
        try {
            rtePhase1.lemmaMatching(entailmentCorpus.getProperties(), lemmaMaxCorrectThres);
            rtePhase1.lemmaPOSMatching(entailmentCorpus.getProperties(), lemmaPOSMaxCorrectThres);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public static void runBleuMatching() throws IOException {
        System.out.println("Bleu matching in progress...");

        XMLParser parser = new XMLParser();

        EntailementCorpus entailementCorpus = parser.getEntailementCorpus("rte2_dev_data/RTE2_dev.xml");

        RTEPhase1 rtePhase1 = new RTEPhase1();

        double maxCorrect = 0;
        double maxCorrectThres = 0.0;
        for(int thres=0; thres<=300; thres++){
            List<Pair> pairs = entailementCorpus.getProperties();
            String predFileName = new String();
            try {
                predFileName = rtePhase1.bleuMatching(pairs, (double)thres/1000);
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }

            double tempCorrect = eval_rte.evaluateWordMatching(pairs, predFileName);
            if(tempCorrect>maxCorrect){
                maxCorrect = tempCorrect;
                maxCorrectThres = (double)thres/1000;
            }
        }

        System.out.println("Bleu matching best correct %: "+maxCorrect*100+"% @ Threshold: "+maxCorrectThres);
        try {
            rtePhase1.wordMatching(entailementCorpus.getProperties(), maxCorrectThres);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public static void runIdfMatching() throws IOException{
        System.out.println("IDF matching in progress...");

        XMLParser parser = new XMLParser();

        EntailementCorpus entailementCorpus = parser.getEntailementCorpus("rte2_dev_data/RTE2_dev.xml");

        RTEPhase1 rtePhase1 = new RTEPhase1();

        double maxCorrect = 0;
        double maxCorrectThres = 0.0;
        for(int thres=0; thres<=100; thres++){
            List<Pair> pairs = entailementCorpus.getProperties();
            String predFileName = new String();
            try {
                predFileName = rtePhase1.idfMatching(pairs, (double)thres/100);
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }

            double tempCorrect = eval_rte.evaluateWordMatching(pairs, predFileName);
            if(tempCorrect>maxCorrect){
                maxCorrect = tempCorrect;
                maxCorrectThres = (double)thres/100;
            }
        }

        System.out.println("IDF matching best correct : "+maxCorrect*100+"% @ Threshold: "+maxCorrectThres);
        try {
            rtePhase1.wordMatching(entailementCorpus.getProperties(), maxCorrectThres);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}
